"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能
Case Name   : 共享存储数据库启动自定义资源时不加-n单独指定节点nodeid,
              系统提示输入有误,退出start操作
Create At   : 2023/03/28
Owner       : opentestcase043
Description :
        1.查询集群状态
        2.停止数据库自定义资源
        3.查询集群状态
        4.启动自定义资源时不加-n单独指定节点nodeid
        5.查询集群状态
        6.清理环境
Expect      :
        1.集群状态正常
        2.停止数据库成功
        3.对应的节点状态offline
        4.系统提示输入有误,退出start操作
        5.对应的节点状态offline
        6.清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

LOG = Logger()
Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorage(unittest.TestCase):

    def setUp(self):
        LOG.info('-----this is setup-----')
        LOG.info(f'-----{os.path.basename(__file__)} start-----')
        self.con = Constant()

    def test_share_storage(self):
        text = '-----step1:查询集群状态; expect:集群状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        res = status.splitlines()[-1].split('|')[0].split('6001')[1]
        self.assertIn('P Primary Normal', res, '执行失败:' + text)

        text = '-----step2:停止数据库自定义资源; expect:停止数据库成功-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('stop', '-n 1 -I 6001')
        LOG.info(status)
        self.assertIn(self.con.cm_stop_resource_success_msg,
                      status, '执行失败:' + text)

        text = '-----step3:查询集群状态; expect:对应的节点状态offline-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        res = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        self.assertIn('P Down    Manually stopped', res, '执行失败:' + text)

        text = '-----step4:启动自定义资源时不加-n单独指定节点nodeid; ' \
               'expect:系统提示输入有误,退出start操作-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('start', '1 -I 6001')
        LOG.info(status)
        self.assertIn('The cmdline entered by the user is incorrect',
                      status, '执行失败:' + text)

        text = '-----step5:查询集群状态; expect:对应的节点状态offline-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        res = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        self.assertIn('P Down    Manually stopped', res, '执行失败:' + text)

    def tearDown(self):
        text = '-----this is tearDown-----'
        LOG.info(text)
        text = '-----step6:清理环境; expect:清理环境成功-----'
        LOG.info(text)
        status1 = Primary_SH.exec_cm_ctl('start')
        status2 = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status1)
        LOG.info(status2)
        self.assertIn(self.con.cm_start_success_msg, status1,
                      '执行失败:' + text)
        self.assertIn(self.con.cm_switchover_success_msg, status2,
                      '执行失败:' + text)
        LOG.info(f'-----{os.path.basename(__file__)} end-----')
